define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格參數配置
            Table.api.init({
                extend: {
                    index_url: 'jdy/feelog/index',
                    add_url: 'jdy/feelog/add',
                    edit_url: 'jdy/feelog/edit',
                    del_url: 'jdy/feelog/del',
                    multi_url: 'jdy/feelog/multi',
                    table: 'jdy_feelog',
                }
            });

            var table = $("#table");

            var apo = "LIKE %...%";
            if (Config.doedit) {
                apo = false;
            }
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showColumns: true,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true, visible:Config.doedit},
                        //{field: 'id', title: __('Id')},
                        {field: 'operate', visible:Config.doedit, title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        //{field: 'anchang_id', title: __('Anchang_id')},
                        {field: 'anchang.name', title: __('Anchang.name'), operate: apo, visible: !Config.doedit},
                        {field: 'pay_date', title: __('Pay_date'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'fee', title: __('Fee')},
                        {
                            field: 'fee',
                            title: __('Fee'),
                            editable: {
                                emptytext: '0',
                                rows:10,
                                //tpl: "<textarea rows='10' cols='50'></textarea>",
                                disabled: !Config.doedit , //true 則不能編輯 ---- 操作權限
                                success: function (response, newValue) {
                                    //$(".btn-refresh").trigger("click");
                                }
                            },
                            operate: false
                        },
                        {field: 'task_id', title: __('Task_id'), operate: false},
                        {field: 'title', title: __('Title')},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.normal},

                        {field: 'fee_type', title: __('Fee_type'), searchList: {"Month":__('每月繳'),"Season":__('季度繳'),"HalfYear":__('半年繳'),"Year":__('年繳')}, formatter:Controller.api.formatter.feetype},
                        {field: 'is_pay', title: __('Is_pay'), searchList: {"0":__('未繳費'),"1":__('已繳費')}, formatter:Controller.api.formatter.ispay},
                        {field: 'opman', title: __('Opman'), operate:false},
                        {field: 'create_time', title: __('Create_time'), operate:false},
                        {field: 'update_time', title: __('Update_time'), operate:false},
                        //{field: 'anchang.id', title: __('Anchang.id')},
                        //{field: 'anchang.building_id', title: __('Anchang.building_id')},
                        //{field: 'anchang.room_id', title: __('Anchang.room_id')},
                        //{field: 'anchang.name', title: __('Anchang.name')},
                        //{field: 'anchang.addr', title: __('Anchang.addr')},
                        //{field: 'anchang.remark', title: __('Anchang.remark')},
                        //{field: 'anchang.contact1_id', title: __('Anchang.contact1_id')},
                        //{field: 'anchang.contact2_id', title: __('Anchang.contact2_id')},
                        //{field: 'anchang.companywindow_id', title: __('Anchang.companywindow_id')},
                        //{field: 'anchang.xianshi', title: __('Anchang.xianshi')},
                        //{field: 'anchang.xzq', title: __('Anchang.xzq')},
                        //{field: 'anchang.mapurl', title: __('Anchang.mapurl'), formatter: Table.api.formatter.url},
                        //{field: 'anchang.orderdate', title: __('Anchang.orderdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'anchang.create_time', title: __('Anchang.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'anchang.update_time', title: __('Anchang.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                    ]
                ]
            });

            //點擊fee_type
            $(document).on("click", ".feetype[data-id]", function () {

                if (Config.doedit) {
                    var url = 'jdy/feelog/changefeetype';
                    var id = $(this).data('id');
                    var _this = $(this);
                    Fast.api.ajax({
                        url: url,
                        data: {ids: id},
                    }, function (data, ret) {
                        var valueA = "每月繳";
                        if (ret.data.fee_type == "Season") {
                            valueA = "季度繳";
                        } else if (ret.data.fee_type == "HalfYear") {
                            valueA = "半年繳";
                        } else if (ret.data.fee_type == "Year") {
                            valueA = "年繳";
                        }
                        _this.html(valueA);
                    });
                }

            });

            //點擊is_pay
            $(document).on("click", ".ispay[data-id]", function () {

                //if (Config.caozuo) {
                    var url = 'jdy/feelog/changeispay';
                    var id = $(this).data('id');
                    var _this = $(this);
                    Fast.api.ajax({
                        url: url,
                        data: {ids: id},
                    }, function (data, ret) {
                        console.log(ret);
                        if (ret.data.is_pay == 0) {
                            _this.removeClass('text-success')
                            _this.addClass('text-danger');
                            _this.html("未繳費");
                        }  else {
                            _this.removeClass('text-danger')
                            _this.addClass('text-success');
                            _this.html("已繳費");
                        }
                    });
                //}

            });

            // 為表格綁定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                feetype: function (value, row, index) {

                    var valueA = "每月繳";
                    if (value == "Season") {
                        valueA = "季度繳";
                    } else if (value == "HalfYear") {
                        valueA = "半年繳";
                    } else if (value == "Year") {
                        valueA = "年繳";
                    }
                    if (!Config.doedit) {
                        return valueA;
                    }

                    return [
                        '<a href="#" class="feetype success" data-id= "' + row.id + '" > ' + valueA + '</a>'
                    ].join("")
                },
                ispay: function (value, row, index) {
                    if (value == 0) {
                        return [
                            '<a href="#" class="ispay text-danger" data-id= "' + row.id + '" > 未繳費 </a>'
                        ].join("")
                    }  else {
                        return [
                            '<a href="#" class="ispay text-success" data-id= "' + row.id + '" > 已繳費 </a>'
                        ].join("")
                    }
                }
            }
        }
    };
    return Controller;
});